package com.htthorizon.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.htthorizon.common.core.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.htthorizon.common.core.utils.StringUtils;
import com.htthorizon.common.core.utils.poi.ExcelUtil;
import com.htthorizon.common.core.web.controller.BaseController;
import com.htthorizon.common.core.web.domain.AjaxResult;
import com.htthorizon.common.core.web.page.TableDataInfo;
import com.htthorizon.common.log.annotation.Log;
import com.htthorizon.common.log.enums.BusinessType;
import com.htthorizon.common.security.annotation.RequiresPermissions;
import com.htthorizon.common.security.utils.SecurityUtils;
import com.htthorizon.system.api.domain.SysDictData;
import com.htthorizon.system.service.ISysDictDataService;
import com.htthorizon.system.service.ISysDictTypeService;

/**
 * 数据字典信息
 * 
 * @author mark
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;
    
    @Autowired
    private ISysDictTypeService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    @Log(title = "系统管理/字典查询", businessType = BusinessType.QUERY,type = Constants.SYSTEM)
    public TableDataInfo list(SysDictData dictData)
    {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }
    @RequiresPermissions("model:dict:query")
    @GetMapping("/bussiness/list")
    @Log(title = "系统管理/业务域查询", businessType = BusinessType.QUERY,type = Constants.SYSTEM)
    public TableDataInfo list2(SysDictData dictData)
    {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList2(dictData);
        return getDataTable(list);
    }


    @Log(title = "系统管理/字典数据", businessType = BusinessType.EXPORT,type = Constants.SYSTEM)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictData dictData)
    {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        util.exportExcel(response, list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return success(dictDataService.selectDictDataById(dictCode));
    }

    @RequiresPermissions("model:dict:query")
    @GetMapping(value = "/bussiness/get/{dictCode}")
    public AjaxResult getInfo2(@PathVariable Long dictCode)
    {
        return success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return success(data);
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "系统管理/字典数据", businessType = BusinessType.INSERT,type = Constants.SYSTEM)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysDictData dict)
    {
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }
//    @RequiresPermissions("system:dict:add")
    @RequiresPermissions("model:dict:add")
    @Log(title = "系统管理/字典数据", businessType = BusinessType.INSERT,type = Constants.SYSTEM)
    @PostMapping("/bussiness/add")
    public AjaxResult add2(@Validated @RequestBody SysDictData dict)
    {
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }
    /**
     * 修改保存字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @Log(title = "系统管理/字典数据", businessType = BusinessType.UPDATE,type = Constants.SYSTEM)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SysDictData dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }
//    @RequiresPermissions("system:dict:edit")
    @RequiresPermissions("model:dict:edit")
    @Log(title = "系统管理/字典数据", businessType = BusinessType.UPDATE,type = Constants.SYSTEM)
    @PostMapping("/bussiness/edit")
    public AjaxResult edit2(@Validated @RequestBody SysDictData dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "系统管理/字典类型", businessType = BusinessType.DELETE,type = Constants.SYSTEM)
    @GetMapping("/del/{dictCodes}")
    public AjaxResult remove(@PathVariable("dictCodes") Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }

//    @RequiresPermissions("system:dict:remove")
    @RequiresPermissions("model:dict:remove")
    @Log(title = "系统管理/字典类型", businessType = BusinessType.DELETE,type = Constants.SYSTEM)
    @GetMapping("/bussiness/delete/{dictCodes}")
    public AjaxResult remove2(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
